import utils.KBHttpUtils;

import java.io.IOException;
import java.util.HashMap;

public class AppLivy {

 private static String LIVY_HOST = "http://10.2.29.105:8999";


    public static void main(String[] args) throws IOException {
        System.setProperty("java.security.krb5.conf", "C:\\Users\\yiyang\\Desktop\\krb5.conf");
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
        System.setProperty("sun.security.krb5.debug", "true"); //Kerberos Debug模式
        System.setProperty("java.security.auth.login.config", "C:\\Users\\yiyang\\Desktop\\login-yarn.conf");
//        UserGroupInformation.loginUserFromKeytab("nifi@TRENDY-GLOBAL.COM", "/etc/security/keytabs/nifi.headless.keytab");

        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Accept", "application/json");
        headers.put("X-Requested-By", "nifi");
        //创建一个交互式会话
        String kindJson = "{\"kind\": \"spark\", \"proxyUser\":\"nifi\"}";
        KBHttpUtils.postAccess(LIVY_HOST + "/sessions", headers, kindJson);
        //执行code
        String code = "{\"code\":\"sc.parallelize(1 to 50000).count()\"}";
        KBHttpUtils.postAccess(LIVY_HOST + "/sessions/2/statements", headers, code);
        KBHttpUtils.deleteAccess(LIVY_HOST + "/sessions/3", headers);
//        封装提交Spark作业的JSON数据
        String submitJob = "{\"className\": \"org.apache.spark.examples.SparkPi\",\"executorMemory\": \"1g\",\"args\": [200],\"file\": \"/fayson-yarn/jars/spark-examples-1.6.0-cdh5.14.0-hadoop2.6.0-cdh5.14.0.jar\"}";
//        向集群提交Spark作业
        KBHttpUtils.postAccess(LIVY_HOST + "/batches", headers, submitJob);
//        通过提交作业返回的SessionID获取具体作业的执行状态及APPID
        KBHttpUtils.getAccess(LIVY_HOST + "/batches/4", headers);



//        String []cmds = {"curl", "-i", "-w", "状态%{http_code}；DNS时间%{time_namelookup}；"
//                + "等待时间%{time_pretransfer}TCP 连接%{time_connect}；发出请求%{time_starttransfer}；"
//                + "总时间%{time_total}","http://www.baidu.com"};
//        ProcessBuilder pb=new ProcessBuilder(cmds);
//        pb.redirectErrorStream(true);
//        Process p;
//        try {
//            p = pb.start();
//            BufferedReader br=null;
//            String line=null;
//
//            br=new BufferedReader(new InputStreamReader(p.getInputStream()));
//            while((line=br.readLine())!=null){
//                System.out.println("\t"+line);
//            }
//            br.close();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
    }
}
